package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.bo.SearchJobsBO;
import com.imooc.enums.JobStatus;
import com.imooc.feign.CompanyMicroServiceFeign;
import com.imooc.feign.UserMicroServiceFeign;
import com.imooc.mapper.JobMapper;
import com.imooc.pojo.Job;
import com.imooc.result.GraceJSONResult;
import com.imooc.service.JobService;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.PageUtils;
import com.imooc.utils.PagedGridResult;
import com.imooc.vo.CompanyInfoVO;
import com.imooc.vo.SearchJobsVO;
import com.imooc.vo.UsersVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * HR发布的职位表 服务实现类
 * </p>
 *
 * @author liansen
 * @since 2023-05-15
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobService {

    @Autowired
    private CompanyMicroServiceFeign companyMicroServiceFeign;

    @Autowired
    private UserMicroServiceFeign userMicroServiceFeign;

    @Override
    public PagedGridResult queryJobList(String hrId, String companyId, Integer page, Integer limit, Integer status) {

        PageHelper.startPage(page, limit);

        LambdaQueryWrapper<Job> wrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNotBlank(hrId)){
            wrapper.eq(Job::getHrId, hrId);
        }

        wrapper.eq(Job::getCompanyId, companyId);

        if(status != null){
            if(status == JobStatus.OPEN.type || status==JobStatus.CLOSE.type || status==JobStatus.DELETE.type){
                wrapper.eq(Job::getStatus, status);
            }
        }

        List<Job> jobs = baseMapper.selectList(wrapper);

        PageInfo<Job> pageInfo = new PageInfo<>(jobs);
        PagedGridResult result = new PagedGridResult();
        result.setRows(pageInfo.getList());
        result.setPage(page);
        result.setTotal(pageInfo.getPages());
        result.setRecords(pageInfo.getTotal());
        return result;
    }

    @Override
    public Job queryJobDetail(String jobId, String hrId, String companyId) {
        LambdaQueryWrapper<Job> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Job::getId, jobId);

        if(StringUtils.isNotBlank(hrId)){
            wrapper.eq(Job::getHrId, hrId);
        }

        if(StringUtils.isNotBlank(companyId)) {
            wrapper.eq(Job::getCompanyId, companyId);
        }

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public void modifyJobStatus(String jobId, String hrId, String companyId, JobStatus status) {
        Job job = new Job();
        job.setStatus(status.type);
        job.setUpdatedTime(LocalDateTime.now());

        LambdaQueryWrapper<Job> wrapper = new LambdaQueryWrapper<Job>().eq(Job::getId, jobId).eq(Job::getHrId, hrId).eq(Job::getCompanyId, companyId);
        baseMapper.update(job, wrapper);
    }

    @Override
    public PagedGridResult searchJobs(SearchJobsBO searchJobsBO, Integer page, Integer limit) {
        String jobName = searchJobsBO.getJobName();
        String jobType = searchJobsBO.getJobType();
        String city = searchJobsBO.getCity();
        Integer beginSalary = searchJobsBO.getBeginSalary();
        Integer endSalary = searchJobsBO.getEndSalary();

        //开启分页
        PageHelper.startPage(page, limit);

        //条件分页查询
        LambdaQueryWrapper<Job> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Job::getStatus, JobStatus.OPEN.type);

        if(StringUtils.isNotBlank(jobName)){
            wrapper.like(Job::getJobName, jobName);
        }

        if(StringUtils.isNotBlank(jobType)){
            wrapper.eq(Job::getJobType, jobType);
        }

        if(StringUtils.isNotBlank(city)){
            wrapper.eq(Job::getCity, city);
        }

        if(beginSalary != null && beginSalary > 0){
            wrapper.ge(Job::getBeginSalary, beginSalary);
        }

        if(endSalary != null && endSalary > 0){
            wrapper.le(Job::getEndSalary, endSalary);
        }

        List<Job> jobs = baseMapper.selectList(wrapper);
        //如果jobs为空, 就不用拼接下面的公司信息和hr信息了
        if(CollectionUtils.isEmpty(jobs)){
            return PageUtils.setterPagedGrid(jobs, page);

        }

        List<SearchJobsVO> searchJobsVOList = jobs.stream().map(job -> {

            SearchJobsVO searchJobsVO = new SearchJobsVO();

            String companyId = job.getCompanyId();
            String userId = job.getHrId();

            //通过公司Id查询公司信息
            GraceJSONResult companyResult = companyMicroServiceFeign.moreInfo(companyId);
            Object data = companyResult.getData();
            String companyInfoVOStr = JsonUtils.objectToJson(data);
            CompanyInfoVO companyInfoVO = JsonUtils.jsonToPojo(companyInfoVOStr, CompanyInfoVO.class);

            //通过HR id获取HR信息
            GraceJSONResult hrInfoResult = userMicroServiceFeign.get(userId);
            Object hrInfoData = hrInfoResult.getData();
            String hrInfoStr = JsonUtils.objectToJson(hrInfoData);
            UsersVO usersVO = JsonUtils.jsonToPojo(hrInfoStr, UsersVO.class);

            BeanUtils.copyProperties(job, searchJobsVO);
            searchJobsVO.setCompanyInfoVO(companyInfoVO);
            searchJobsVO.setUsersVO(usersVO);

            return searchJobsVO;
        }).collect(Collectors.toList());

        return PageUtils.setterPagedGrid(searchJobsVOList, page);
    }
}
